package cn.enjoy;

import cn.enjoy.service.IProductClientService;
import cn.enjoy.service.IZUUlClientService;
import cn.enjoy.vo.Product;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/consumer")
public class ConsumerProductController {
    // 未加入ribbon 之前
//    public static final String PRODUCT_GET_URL = "http://localhost:8080/prodcut/get/";
//    public static final String PRODUCT_LIST_URL="http://localhost:8080/prodcut/list/";
//    public static final String PRODUCT_ADD_URL = "http://localhost:8080/prodcut/add/";

    // feign 未引入之前
//    public static final String PRODUCT_GET_URL = "http://MICROCLOUD-PROVIDER-PRODUCT/prodcut/get/";
//    public static final String PRODUCT_LIST_URL="http://MICROCLOUD-PROVIDER-PRODUCT/prodcut/list/";
//    public static final String PRODUCT_ADD_URL = "http://MICROCLOUD-PROVIDER-PRODUCT/prodcut/add/";

//    @Resource
//    private RestTemplate restTemplate;
//
//    @Resource
//    private HttpHeaders httpHeaders;
//
//    @Resource
//    private LoadBalancerClient loadBalancerClient;

    @Resource
    private IProductClientService iProductClientService;

    //使用到zuul 引入
    @Resource
    private IZUUlClientService izuUlClientService;


    @RequestMapping("/product/get")
    public Object getProduct(long id) {
        // product 服务未加密前
       // Product product = restTemplate.getForObject(PRODUCT_GET_URL + id, Product.class);

        // feign 未引入之前
//        Product product = restTemplate.exchange(PRODUCT_GET_URL + id, HttpMethod.GET,new HttpEntity
//                <Object>(httpHeaders), Product.class).getBody();
//        return  product;

        // feign 引入之后
        return iProductClientService.getProduct(id);
    }

    @RequestMapping("/product/list")
    public  Object listProduct() {
        // product 服务未加密前
       // List<Product> list = restTemplate.getForObject(PRODUCT_LIST_URL, List.class);

        // feign 未引入之前
        // 在消费方获得被调用服务的信息
//        ServiceInstance serviceInstance = this.loadBalancerClient.choose("MICROCLOUD-PROVIDER-PRODUCT") ;
//        System.out.println(
//                "【*** ServiceInstance ***】host = " + serviceInstance.getHost()
//                        + "、port = " + serviceInstance.getPort()
//                        + "、serviceId = " + serviceInstance.getServiceId());
//
//        List<Product> list = restTemplate.exchange(PRODUCT_LIST_URL,HttpMethod.GET,new HttpEntity<Object>(httpHeaders), List.class).getBody();
//        return  list;

        // feign 引入之后
        return iProductClientService.listProduct();
    }

    @RequestMapping("/product/add")
    public Object addPorduct(Product product) {
        // product 服务未加密前
        //Boolean result = restTemplate.postForObject(PRODUCT_ADD_URL, product, Boolean.class);

        // feign 未引入之前
//        Boolean result = restTemplate.exchange(PRODUCT_ADD_URL, HttpMethod.POST,new HttpEntity<Object>(product,httpHeaders), Boolean.class).getBody();
//        return  result;

        // feign 引入之后
        return iProductClientService.addPorduct(product);
    }

    // 加入zuul网关后访问的接口
    @RequestMapping("/product/getProductAndUser")
    public Object getProductAndUser(long id) {
        Map<String,Object> result = new HashMap();
        result.put("product",izuUlClientService.getProduct(id));
        result.put("user",izuUlClientService.getUsers(id+""));
        return  result;
    }
}
